<template>
	<view class="container">
		<u-navbar title="创建简历2/2" placeholder></u-navbar>
		<u-cell-group>
			<u-cell-item required isLink title="学校名称" :title-style="data.titleStyle" :use-label-slot="true" 
				:arrow="false">
				<template v-slot:label>
					<u-field :border-bottom="false" label-width="0" 
						:field-style="data.formData.school ? data.labelStyle:data.labelStyle2"
						v-model="data.formData.school" placeholder="请输入学校名称" :placeholder-style="data.placeholderStyle"
						 />
				</template>
			</u-cell-item>
			<u-cell-item required isLink title="专业" :title-style="data.titleStyle" :use-label-slot="true"
				:arrow="false">
				<template v-slot:label>
					<u-field :border-bottom="false" label-width="0"
						:field-style="data.formData.major ? data.labelStyle:data.labelStyle2"
						v-model="data.formData.major" placeholder="请输入专业" :placeholder-style="data.placeholderStyle"
						 />
				</template>
			</u-cell-item>

			<u-cell-item required isLink @click="data.iseducation=true" title="获得学历" :title-style="data.titleStyle"
				:label="data.formData.education_text || '请选择学历'"
				:label-style="data.formData.education_text ? data.labelStyle:data.labelStyle2">
			</u-cell-item>

			<u-cell-item required isLink @click="data.isstarttime=true" title="入学时间" :title-style="data.titleStyle"
				:label="data.formData.starttime || '请选择入学时间'"
				:label-style="data.formData.starttime ? data.labelStyle:data.labelStyle2">
			</u-cell-item>

			<u-cell-item required isLink title="" :title-style="data.titleStyle" :use-label-slot="true" :arrow="false">
				<template v-slot:label>
					<view style="display: flex;justify-content:space-between;">
						<input disabled @click="data.isendtime=true" type="text" :style="data.labelStyle"
							:value="data.formData.endtime?$u.timeFormat(new Date(data.formData.endtime), 'yyyy-mm') : data.dateshow"
							placeholder="请选择毕业时间" :placeholder-style="data.placeholderStyle">
						<u-icon name="arrow-right" @click="data.isendtime=true"></u-icon>
					</view>
				</template>
				<template v-slot:title>
					<view class="titleleft" @click.stop>
						<text>毕业时间</text>
						<u-checkbox shape="circle" @change="enterjobtime" v-model="data.thisyear"><text
								style="white-space: nowrap !important;">应届生/无经验</text></u-checkbox>
					</view>
				</template>
			</u-cell-item>
		</u-cell-group>
		<u-bottom-button @cancel='getfrist' :showCancelButton="data.StepsShow" cancelText="上一步"
			confirmText="保存" @confirm="$u.throttle(submit,2000)"></u-bottom-button>
		<!-- <u-bottom-button confirmText="保存" @confirm="submit"></u-bottom-button> -->
		<!-- 学历 -->
		<u-picker v-model="data.iseducation" @confirm="confirm" range-key="name" mode="selector"
			:range="data.educationList" :default-selector="data.defaultIndex"></u-picker>
		<!-- 入学时间 -->
		<u-picker v-model="data.isstarttime" :params="data.params" @confirm="starttimeConfirm"
			:default-time="data.formData.starttime?$u.timeFormat(new Date(data.formData.starttime), 'yyyy-mm-dd'):$u.timeFormat(new Date(), 'yyyy-mm-dd')"
			mode="time"></u-picker>
		<!--毕业时间 -->
		<u-picker v-model="data.isendtime" :params="data.params" @confirm="endtimeConfirm"
			:default-time="data.formData.endtime?$u.timeFormat(new Date(data.formData.endtime), 'yyyy-mm-dd'):$u.timeFormat(new Date(), 'yyyy-mm-dd')"
			mode="time"></u-picker>

	</view>
</template>

<script setup>
	import {
		ref,
		reactive
	} from "vue"
	import {
		onReady,
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	const form1 = ref();
	const data = reactive({
		StepsShow:true,
		params: {
			year: true,
			month: true,
			day: false,
			hour: false,
			minute: false,
			second: false
		},

		isNav: true,
		rules: {
			school: [{
				required: true,
				message: '请输入学校名称',
				// 可以单个或者同时写两个触发验证方式 
				trigger: 'blur',
			}],
			major: [{
				required: true,
				message: '请输入专业',
				// 可以单个或者同时写两个触发验证方式 
				trigger: 'change',
			}],
			education_text: [{
				required: true,
				message: '请选择学历',
				// 可以单个或者同时写两个触发验证方式 
				trigger: 'change',
			}],
			starttime: [{
				required: true,
				message: '请选择入学时间',
				// 可以单个或者同时写两个触发验证方式 
				trigger: 'change',
			}],
			endtime: [{
				required: true,
				message: '请选择毕业时间',
				// 可以单个或者同时写两个触发验证方式 
				trigger: 'change',
			}]

		},
		formData: {
			school: '',
			education_text: '',
			education: '',
			starttime: "",
			endtime: "",
		},
		iseducation: false,
		isstarttime: false,
		isendtime: false,
		educationList: [],
		defaultIndex: [0],
		thisyear: false,
		titleStyle: {
			'font-size': '30rpx',
			'color': '#303133'
		},
		labelStyle: {
			'font-size': '30rpx',
			'color': '#000'
		},
		labelStyle2: {
			'font-size': '28rpx',
			'color': '#c0c4cc'
		},
		placeholderStyle: "font-size: 28rpx;color:#c0c4cc",
		dateshow:""
	})
	
	const getfrist = ()=>{
		uni.$u.route({
			type: 'back'
		})
	}

	// 获取学历列表
	const getEducationList = async () => {
		let params = {
			type: 'education'
		}
		let res = await uni.$u.api.getClassifyInfo({
			params
		})
		if (res) {
			data.educationList = res;
			let cur = data.educationList.findIndex(item => {
				return item.name == data.formData.education_text
			})
			if (cur != -1) {
				data.defaultIndex = [cur]
			}

		}
	}
	// 用户在线简历数据
	const getUserdetail = async () => {
		let res = await uni.$u.api.getOnlineMange({})
		if (res) {
			if(res.education_list.length>0){
				data.formData.school = res.education_list[0].school
				data.formData.major = res.education_list[0].major
				data.formData.starttime = uni.$u.timeFormat(new Date(res.education_list[0].starttime * 1000),
					'yyyy-mm')
				data.formData.endtime = uni.$u.timeFormat(new Date(res.education_list[0].endtime * 1000), 'yyyy-mm')
			}
			
			if (res.education_list[0].todate == 1) {
				data.thisyear = true
				data.formData.endtime = 0
				data.dateshow = uni.$u.timeFormat(new Date(), 'yyyy-mm')
			}
			data.formData.education_text = res.education_list[0].education_text
			data.formData.education = res.education_list[0].education
		}
	}
	// 修改最高学历
	const confirm = (e) => {
		let index = e[0]
		data.formData.education_text = data.educationList[index].name;
		data.formData.education = data.educationList[index].id;
	}
	const enterjobtime = (e) => {
		
		if (e.value) {
			data.formData.endtime = 0
			data.dateshow = uni.$u.timeFormat(new Date(), 'yyyy-mm')
			data.formData.todate = 1
		} else {
			data.formData.todate = 0
			data.formData.endtime = 0
			data.dateshow = ""
		}
	}

	const starttimeConfirm = (e) => {
		data.formData.starttime = e.year + '-' + e.month
	}
	const endtimeConfirm = (e) => {
		data.formData.endtime = e.year + '-' + e.month
		let nowdate = uni.$u.timeFormat(new Date(), 'yyyy-mm')
		if (data.formData.endtime == nowdate) {
			data.thisyear = true;
			data.formData.todate = 1
			data.dateshow = uni.$u.timeFormat(new Date(), 'yyyy-mm')
			data.formData.endtime = 0
		} else {
			data.thisyear = false;
			data.formData.todate = 0
		}
	}
	// 保存
	const submit = async () => {

		let {
			school,
			major,
			education_text,
			starttime,
			endtime
		} = data.formData

		if (!school) {
			return uni.showToast({
				title: '请输入学校名称',
				icon: "none"
			})
		}
		if (!major) {
			return uni.showToast({
				title: '请输入专业',
				icon: "none"
			})
		}
		if (!education_text) {
			return uni.showToast({
				title: '请选择学历',
				icon: "none"
			})
		}
		if (!starttime) {
			return uni.showToast({
				title: '请选择入学时间',
				icon: "none"
			})
		}

		if (!endtime  && !data.thisyear) {
			return uni.showToast({
				title: '请选择毕业时间',
				icon: "none"
			})
		}
		let res = await uni.$u.api.stepNoWork(data.formData)
		if (res) {
			uni.$u.toast('添加成功,请继续完善简历')
			setTimeout(() => {
				uni.removeStorageSync('next_code');
				uni.switchTab({
					//关闭当前页面，跳转到应用内的某个页面。
					url: '/pages/home/home'
				});
			}, 500)
		}
	}
	onReady(() => {})
	onLoad(() => {
		// ios侧滑返回功能（关闭）
		var pages = getCurrentPages();
		var page = pages[pages.length - 1];
		// #ifdef APP-PLUS
		var currentWebview = page.$getAppWebview();
		currentWebview.setStyle({
			popGesture: 'none'
		});
		// #endif
		getEducationList()
		getUserdetail()

	})
</script>

<style lang="scss">
	page,
	body {
		width: 100vw;
		height: 100%;
		background-color: #fff;
	}

	.container {
		// padding: 30rpx;


		.delete {
			height: 50rpx;
			line-height: 50rpx;
			text-align: center;

			text {
				color: #867F7F;
				font-size: 24rpx;
			}
		}
	}

	:deep .u-flex-1 {
		margin-left: 0 !important;
	}

	:deep .u-field__input-wrap {
		width: 500rpx !important;
	}

	.titleleft {
		width: calc(100vw - 64rpx);
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 100rpx;
	}
	:deep .u-cell{
		height: 200rpx;
	}
	:deep .u-cell--required{
		&::before{
			top: 24px!important;
		}
		
	}
</style>